#!/bin/sh
#
# Copyright (C) 2012 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# This script attempts to test Flashrom partial read capability
#

. "./common.sh"

logfile="${0}.log"
partially_filled_image="partially_filled_image.bin"
region_size=4096

partial_reads_fail()
{
	echo "$*" >> ${logfile}
	echo "$0: failed" >> ${logfile}
	exit ${EXIT_FAILURE}
}

which cmp > /dev/null || partial_writes_fail "cmp is required to use this script"

num_regions=16
include_regions=""

# Generate a layout and a string with "-i" arguments.
for i in `seq 0 $((num_regions - 1))` ; do
	start=$((i * region_size))
	end=$((i * region_size + region_size - 1))
	printf "0x%06x:0x%06x region_%d\n" ${start} ${end} ${i} >> layout.txt
	include_regions="${include_regions} -i region_${i}:region_${i}.bin"
done

# do all partial reads and generate full-sized partially filled file
do_test_flashrom -l layout.txt ${include_regions} -r ${partially_filled_image}

# check that full-size image matches the size of the backup image
partially_filled_image_size=$(stat --printf="%s\n" ${partially_filled_image})
backup_size=$(stat --printf="%s\n" ${BACKUP})
printf "%s size ? %s ..." ${partially_filled_image} ${backup_size} >> ${logfile}
if [ "$partially_filled_image_size" != "$backup_size" ]; then
	echo "no." >> ${logfile}
	partial_reads_fail "full-size image has incorrect size"
else
	echo "yes." >> ${logfile}
fi

# check that all regions came out the correct size
for i in `seq 0 $((${num_regions} - 1))` ; do
	region_size=$(stat --printf="%s\n" region_${i}.bin)
	printf "region_%d size ?= %d ... " ${i} ${region_size} >> ${logfile}
	if [ "${region_size}" != "${region_size}" ]; then
		echo "no." >> ${logfile}
		partial_reads_fail "region_${i}.bin has incorrect size"
	else
		echo "yes." >> ${logfile}
	fi
done

# check correctness of each region individually
for i in `seq 0 $((${num_regions} - 1))` ; do
	# get region from old image
	dd if=${BACKUP} of=old_region.bin bs=${region_size} count=1 skip=${i} 2>/dev/null

	# compare content in ROM-sized image generated by -r argument
	dd if=${partially_filled_image} of=new_region.bin bs=${region_size} count=1 skip=${i} >/dev/null 2>&1
	cmp -s old_region.bin new_region.bin
	if [ $? -ne 0 ]; then
		partial_reads_fail "partial read test when comparing full image"
	fi

	# compare 4k file generated by -i argument
	cmp -s old_region.bin region_${i}.bin
	if [ $? -ne 0 ]; then
		partial_reads_fail "failed partial read test when comparing region_${i}.bin"
	fi

	echo "echo partial read of region ${i} passed" >> ${logfile}
done

# test that a full read is done if no -i options are used
do_test_flashrom -l layout.txt -r full_image.bin
cmp -s ${BACKUP} full_image.bin
if [ $? -ne 0 ]; then
	partial_reads_fail "produced corrupt image when no -i options are provided"
fi
rm -f full_image.bin

# specify a region that is not likely to exist
do_test_flashrom -l layout.txt -i this_region_does_not_exist -r full_image.bin
if [ $? -eq 0 ]; then
	partial_reads_fail "flashrom did not quit when non-existent region specified"
fi

return "$EXIT_SUCCESS"
